package com.inscream.app.profileTab;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.inscream.app.HomeActivity;
import com.inscream.app.JSONParser;
import com.inscream.app.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class MyNotifications extends Activity {

    String whoisntf, ntfDate, ntfContent, ntfImg;

    ListView myNotificationList;
    private static String ntfurl = "http://mehmetalp.com.tr/seslitwitter/homepage.php?method=5";
    private static final String TAG_OS = "posts";
    private static final String soundRecordTag = "soundRecordTag";

    JSONArray android = null;
    ArrayList<HashMap<String, String>> notificationList = new ArrayList<HashMap<String, String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_notifications);

        new getMyNotifications().execute();
    }



    private class getMyNotifications extends AsyncTask<String, String, JSONObject>{
        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute(){
            super.onPreExecute();

            pDialog = new ProgressDialog(MyNotifications.this);
            pDialog.setMessage("Wait a min");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected JSONObject doInBackground(String... args) {
            JSONParser jsonParser = new JSONParser();
            JSONObject ttjson = jsonParser.getJSONFromUrl(ntfurl);
            return ttjson;
        }

        @Override
        protected void onPostExecute(JSONObject json){
            pDialog.dismiss();
            try {
                android = json.getJSONArray(TAG_OS);
                for(int i = 0; i< android.length(); i++){
                    JSONObject c = android.getJSONObject(i);
                    String getTTListname = c.getString(soundRecordTag);
                    HashMap<String, String> ttmap = new HashMap<String, String>();
                    ttmap.put(soundRecordTag, getTTListname);

                    notificationList.add(ttmap);
                    myNotificationList = (ListView) findViewById(R.id.myNotificationList);
                    ListAdapter myNewAdapter = new SimpleAdapter(MyNotifications.this, notificationList,R.layout.notificationview, new String[]{soundRecordTag}, new int[]{R.id.whoisntf});
                    myNotificationList.setAdapter(myNewAdapter);
                    myNotificationList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) {
                            Toast.makeText(MyNotifications.this, "You Clicked at " + notificationList.get(+position).get("soundRecordTag"), Toast.LENGTH_SHORT).show();
                            String getThisTTItem = notificationList.get(+position).get("soundRecordTag").toString();
                            Toast.makeText(MyNotifications.this, "You Clicked at " + getThisTTItem, Toast.LENGTH_SHORT).show();
                            Intent a = new Intent(MyNotifications.this, HomeActivity.class);
                            a.putExtra("getThisTTItem", getThisTTItem);
                            startActivity(a);
                        }
                    });
                }

            } catch (JSONException e){
                e.printStackTrace();
            }
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu)  {

            // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my_notifications, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
